<?php

/**
 * Description: 前台基佬类控制器
 * @Author: Tudo<997470395@qq.com>
 * @Created: 2015-3-31 下午2:41:05
 */
namespace Home\Controller;

use Think\Controller;

class BaseController extends Controller
{
    public $userInfo = array();
    protected $pageSize = 5;
    protected $totalRows = 0;
    protected $outPut = array();
    protected $p = 1;

    public function __construct() {
        parent::__construct();
        //网站初始化函数
        $this->_initWeb();
    }

    private function _initWeb() {
        //初始化登陆信息
        if (false != ($user = $this->getInitUserInfo())) {
            $this->userInfo = $user;

            $this->outPut['user_info'] = $this->userInfo;
        }

        //初始化分页信息
        $this->p = I('get.p', 1, 'intval');
        //初始化 栏目信息
        $this->outPut['channel'] = $this->getChannel();
        $this->outPut['title'] = C("seo_title");
        $this->assign($this->outPut);


    }

    /** *初始化登陆信息** */
    protected function initLogin($uid) {
        $uModel = M('user');
        $user = $uModel->where(array('id' => $uid))->find();
        if (empty($user)) return false;
        unset($user['password']);
        $map['last_login_time'] = time();
        $map['login_num'] = $user['login_num'] + 1;
        $uModel->where(array('id' => $uid))->data($map)->save();
        $user['login_num'] += 1;
        cookie('user', $user, array('expire' => 3600 * 24 * 30, 'prefix' => 'u_'));

    }

    protected function pages() {
        $page = new \Home\Com\Page($this->totalRows, $this->pageSize);
        return $page->show();
    }

    protected function getCount($table, $where) {
        return M($table)->where($where)->count();
    }

    protected function utilLimit($p) {

        return "limit " . (($this->p - 1) * ($this->pageSize)) . ", {$this->pageSize} ";
    }

    /**检测登陆**/
    protected function checkLogin($password, $email) {
        $user = M('user')->where(array('email' => $email))->find();
        if (empty($user)) return array('code' => -1, 'msg' => "用户不存在！");
        if ($user['password'] != crpypassword($password)) return array("code" => -1, 'msg' => "密码不正确！");
        $this->initLogin($user['id']);
        return array("code" => 1, "msg" => "登陆成功！");
    }

    protected function getInitUserInfo() {
        $user = cookie('u_user');
        if (empty($user)) return false;
        return $user;
    }

    public function area($ajax = true, $region = null) {
        $parentid = I("get.region", 1, 'intval');
        $parentid = ($region !== null) ? $region : $parentid;
        $citys = M('region')->where(array(
            'parent_id' => $parentid
        ))->select();
        if ($ajax == false) return $citys;
        $html = "";
        foreach ($citys as $k => $v) {
            $html .= "<option value='" . $v['region_id'] . "' >" . $v['region_name'] . "</option>";
        }

        echo json_encode($html);
    }

    public function getChannel() {

        $channel = M('category')->where(array('status' => 1))->order('sort desc')->limit(20)->select();
        // print_r($channel);
        return $channel;
    }

    public function initCity() {
        $province = $this->area(false);
        $city = $this->area(false, $province[0]['region_id']);
        $country = $this->area(false, $city[0]['region_id']);
        //print_r($country);
        return array(
            'province' => $province, 'citys' => $city, 'country' => $country
        );
    }

    public function response($outPut, $isJson = false) {
        if ($isJson == false) {
            switch ($outPut['status']) {
                case 0:
                    $this->error($outPut['msg']);
                    break;
                case 1:
                    $this->success($outPut['msg']);
            }
        } else {
            $this->ajaxReturn($outPut, "JSON");
        }
    }
}